'''
Author: your name
Date: 2020-10-27 09:34:25
LastEditTime: 2020-11-04 08:22:42
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \ai20201027\test.py
'''
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression
import numpy as np
from sklearn.metrics import mean_squared_error,r2_score

data = pd.read_csv('generated_data.csv')

# data赋值
x = data.loc[:,'x']
y = data.loc[:,'y']


# plt.figure()
# plt.scatter(x,y)
# plt.show()

# 单因子线性回归模型
lr_model = LinearRegression()
x = np.array(x)
x = x.reshape(-1,1)#转换为多行1列的二维数组
y = np.array(y)
y = y.reshape(-1,1)

# 创建模型和获取预测值
lr_model.fit(x,y)

# 预测目标值
y_predict = lr_model.predict(x)
y_3 = lr_model.predict([[3.5]])
# print(y_3)

# 展示相关因子
k = lr_model.coef_
b = lr_model.intercept_
# print(k,b)

# 评估模型
MSE = mean_squared_error(y,y_predict)
R2 = r2_score(y,y_predict)
# print(MSE,R2)

plt.figure()
plt.plot(y,y_predict)
plt.show()